﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SmartSpace.WebApp._console.page
{
    public partial class page_design : System.Web.UI.Page
    {
        private Guid _pageId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreInit(EventArgs e)
        {
            _pageId = new Guid(Request.QueryString["ss:pageid"]);

            _ctlWebPartManager.Personalization.InitialScope = PersonalizationScope.Shared;

            ICoreService service = ServiceLocator.Get<ICoreService>();
            SmartSpace.Core.Page page = service.GetPage(SsSite.Current.Id, _pageId);
            _ctlTextPage.Text = string.Format("{0}[{1}]", page.Caption, page.Code);
            _ctlTextPage.NavigateUrl = string.Format("{0}{1}", SsSite.HostUrl, page.Code);

            _ctlPanel.Controls.Add(this.LoadControl("~/Templates/Page/" + page.Template.Code + ".ascx"));

            (CatalogZone1.Controls[0] as DeclarativeCatalogPart).WebPartsTemplate = new CategoryTemplate(this, _ctlWebPartManager);


            base.OnPreInit(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

        protected override void FrameworkInitialize()
        {
            base.FrameworkInitialize();
        }

        protected void _ctlBtnEdit_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.EditDisplayMode;
        }

        protected void _ctlBtnCategory_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.CatalogDisplayMode;
        }

        protected void _ctlBtnDesign_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.DesignDisplayMode;
        }

        protected void _ctlBtnBroswer_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.BrowseDisplayMode;
        }
    }

    public class CategoryTemplate : ITemplate
    {
        public System.Web.UI.Page _page;
        private WebPartManager _webPartManager;

        public CategoryTemplate(System.Web.UI.Page page, WebPartManager webPartManager)
        {
            _page = page;
            _webPartManager = webPartManager;
        }

        public void InstantiateIn(Control container)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            PagedList<Module> modules = service.GetModules(SsSite.Current.Type, 0, 999);

            if (modules.Count == 0)
            {
                LoadTestParts1(container);
                LoadTestParts2(container);
            }
            else
            {
                foreach (var module in modules)
                {
                    foreach (var view in module.Views)
                    {
                        Logger.Debug(string.Format("page design - load module[{0}][{1}]  part[{2}][{3}]", module.Caption, module.Code, view.Caption, view.Code));
                        SsPart control = _page.LoadControl(view.Code) as SsPart;
                        
                        if (control != null)
                        {
                            control.ID = view.Id.ToString();

                            WebPart part = _webPartManager.CreateWebPart(control);
                            part.Title = view.Caption;
                            part.Description = view.Caption;
                            container.Controls.Add(part);
                        }
                        else
                        {
                            Logger.Error(string.Format("page design - load module[{0}][{1}]  part[{2}][{3}] error path={4}", module.Caption, module.Code, view.Caption, view.Code, string.Format("~\\_modules\\{0}\\{1}", module.Code, view.Code)));
                        }
                    }
                }
            }
        }

        private void LoadTestParts1(Control container)
        {
            Control control = _page.LoadControl("~/_modules/_system/accountdetail.ascx");
            control.ID = "ctl_accountDetail";// +Guid.NewGuid().ToString();

            WebPart part = _webPartManager.CreateWebPart(control);
            part.ID = "part_accountDetail";
            //part.ChromeType = PartChromeType.None;
            
            part.Title = "accountdetail";
            part.Description = "accountdetail";
            container.Controls.Add(part);
        }

        private void LoadTestParts2(Control container)
        {
            Control control = _page.LoadControl("~/_modules/_system/accountlog.ascx");
            control.ID = "ctl_accountLog";// +Guid.NewGuid().ToString();

            WebPart part = _webPartManager.CreateWebPart(control);
            part.ID = "part_accountLog";
            //part.ChromeType = PartChromeType.None;

            part.Title = "accountlog";
            part.Description = "accountlog";
            container.Controls.Add(part);
        }
    }
}